ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
another minor problem with select, and one i cant seem to find an answer for.
i have a little menu bash script, that i have listed below, thats using subroutines to generate some menu's. it actually works very well, with one minor problem, when the dmenu subroutine returns to the mainmenu subroutine, all i have displayed is the #? prompt, it doesnt show the mainmenu menu choices. is there a way to "force" select to re-display its menu?
ive given an example of the output below the script, i think the example explains whats happenning better than i can with words.
the script.....
Code:
#!/bin/bash
#####
#
# the menu
#
#####
#mainmenu subroutine
function mainmenu {
select menusel in "perform backup" "HS server administration" "DU server administration" "Multi server administration" "Shell administration" "" "Logout" "command line" "HELP!" ; do
if [ "$menusel" = "perform backup" ] ; then
echo "you chose option 1"
fi
if [ "$menusel" = "HS server administration" ] ; then
echo "you chose option 2"
dtype=hs
dmenu
fi
if [ "$menusel" = "DU server administration" ] ; then
echo "you chose option 3"
dtype=du
dmenu
fi
if [ "$menusel" = "Multi server administration" ] ; then
echo "you chose option 4"
dtype=multi
dmenu
fi
if [ "$menusel" = "Shell administration" ] ; then
echo "you chose option 5"
fi
if [ "$menusel" = "Logout" ] ; then
echo "you chose option 8"
fi
if [ "$menusel" = "command line" ] ; then
echo "you chose option 9"
return
fi
if [ "$menusel" = "HELP!" ] ; then
echo "i wish!"
fi
done
}
#dmenu subroutine
function dmenu {
select dmenusel in "change password" "cycle server" "go back"; do
if [ "$dmenusel" = "change password" ] ; then
echo "changing password for $dtype"
fi
if [ "$dmenusel" = "cycle server" ] ; then
echo "cycling server for $dtype"
fi
if [ "$dmenusel" = "go back" ] ; then
return
fi
done
}
mainmenu
ok, thats the script...
this is the output. i have my keyboard entry in bold, to help distinguish it from the script output...
Code:
[zidane@bluemist menu]$ ./dmenu
1) perform backup 6)
2) HS server administration 7) Logout
3) DU server administration 8) command line
4) Multi server administration 9) HELP!
5) Shell administration
#? 2
you chose option 2
1) change password
2) cycle server
3) go back
#? 3<----- here, it returns to the mainmenu routine
#? 8<----- and here, i select from the main menu, but no "menu text" is shown
you chose option 8
[zidane@bluemist menu]$
wht i am trying to do, is have the main menu text shown where i have indicated, but i cant seem to work out how to "force" select to display its menu.
does anyone know how i can make select show its menu text when the submenu subroutine returns? ive been fiddling with this all day yesterday, and most of the morning, and i just cant seem to figure it out.
Originally posted by jschiwal I think that you either want to use the tput command and roll your own menu, or put the select command in a loop, and end the commands with a break;.
put the select command in a loop and end with a break? im not sure what you mean by that, could you expand a little further for me?
If you want to display the menu again, call the mainmenu function, e.g.-
Code:
if [ "$menusel" = "perform backup" ] ; then
echo "you chose option 1"
mainmenu
fi
If you want to make your code more efficient, use "elif" instead of "if", otherwise your script will iterate through all the options even if the first one matches. Better still, use:-
i had considered putting the mainmenu command in there, but surely that would mean the script has multiple subroutines to back out of, with each one needing a "go back" command?
like: mainmenu -> submenu -> mainmenu -> submenu
with each of the routines needing the user to select the relevant "go back" option?
i guess the exit on the mainmenu routine would allow the user to break out of the chain without having to back up, but im a little concerned at having them stacked like that. it seems..... 'messy' (for want of a better word)
I think im going to be using the case matching thing. as you can no doubt tell, im a relative noob when it comes to shell scripts, but i think im getting the hang of it now. the case thing looks like what i was origonally looking for before i just used if.
on the whole, ive a feeling that it might just be easier to write my own menu system, rather than using select. i think im quickly discovering limitations in select that are going to make my life hard, when i can just create some menu functions and have it work perfectly.
i had considered putting the mainmenu command in there, but surely that would mean the script has multiple subroutines to back out of, with each one needing a "go back" command?
Yes, but there is a way round that.
Ignoring the second function for the moment, I think this is what you're looking for:-
Code:
#!/bin/bash
#####
#
# the menu
#
#####
#mainmenu subroutine
function mainmenu {
select menusel in "perform backup" "HS server administration" "DU server administration" "Multi server administration" "Shell administration" "" "Logout" "command line" "HELP!" "EXIT PROGRAM"; do
case $menusel in
"perform backup")
echo "you chose option 1" ;;
"HS server administration")
echo "you chose option 2" ;;
"DU server administration")
echo "you chose option 3" ;;
"Multi server administration")
echo "you chose option 4" ;;
"Shell administration")
echo "you chose option 5" ;;
"Logout")
echo "you chose option 8" ;;
"command line")
echo "you chose option 9" ;;
"HELP!")
echo "i wish!" ;;
"EXIT PROGRAM")
exit 0 ;;
esac
break
done
}
while true; do mainmenu; done
The "break" after the "case" block will make sure "select" exits after every selection, but the "while true; do" will simply call the "mainmenu" function again.
The "EXIT PROGRAM" option is there to stop the infinite loop, which you may or may not want to add.
ahh, yeah, that looks like its taking some serious shape there.
i think the while and case are the way to go, tbh im not sure select is what i need for this, for all the time its taken me to figure out how to bend it to my will, i could have just written a menu from scratch. whilst select is usefull for "quick and dirty" menu selections, i think something like this is gonna need me to get my hands dirty and make it myself.
so far im working on the theory of:
while var is true, do
display menu
read
case for answers
wend.
or similar, it seems a pretty solid way to go with it. much like your example, only with a man-made menu instead of select.
thanks for all the help guys, it seems like select is just not what i was looking for on this one, but it was good to see the different ways of using it that you suggested looks like its back to the drawing board on this one, but as they say, all experience is good experience.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.